package cn.iocoder.yudao.module.xiaoBizHandler.controller.admin.tbGenInstance;

import cn.iocoder.yudao.framework.common.genId.PKGenerator;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;

import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.xiaoBizHandler.controller.admin.tbGenInstance.vo.*;
import cn.iocoder.yudao.module.xiaoBizHandler.dal.dataobject.tbGenInstance.XTbGenInstanceDO;
import cn.iocoder.yudao.module.xiaoBizHandler.convert.tbGenInstance.XTbGenInstanceConvert;
import cn.iocoder.yudao.module.xiaoBizHandler.service.tbGenInstance.XTbGenInstanceService;

@Api(tags = "管理后台 - 代码生成示例")
@RestController
@RequestMapping("/xiaoBizHandler/XT-b-gen-instance")
@Validated
public class XTbGenInstanceController {

    @Resource
    private XTbGenInstanceService xTbGenInstanceService;

    @PostMapping("/create")
    @ApiOperation("创建代码生成示例")
    @PreAuthorize("@ss.hasPermission('xbh:XT-b-gen-instance:create')")
    public CommonResult<String> createXTbGenInstance(@Valid @RequestBody XTbGenInstanceCreateReqVO createReqVO) {
        createReqVO.setId(PKGenerator.newId());
        return success(xTbGenInstanceService.createXTbGenInstance(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新代码生成示例")
    @PreAuthorize("@ss.hasPermission('xbh:XT-b-gen-instance:update')")
    public CommonResult<Boolean> updateXTbGenInstance(@Valid @RequestBody XTbGenInstanceUpdateReqVO updateReqVO) {
        xTbGenInstanceService.updateXTbGenInstance(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除代码生成示例")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('xbh:XT-b-gen-instance:delete')")
    public CommonResult<Boolean> deleteXTbGenInstance(@RequestParam("id") String id) {
        xTbGenInstanceService.deleteXTbGenInstance(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得代码生成示例")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('xbh:XT-b-gen-instance:query')")
    public CommonResult<XTbGenInstanceRespVO> getXTbGenInstance(@RequestParam("id") String id) {
        XTbGenInstanceDO xTbGenInstance = xTbGenInstanceService.getXTbGenInstance(id);
        return success(XTbGenInstanceConvert.INSTANCE.convert(xTbGenInstance));
    }

    @GetMapping("/list")
    @ApiOperation("获得代码生成示例列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('xbh:XT-b-gen-instance:query')")
    public CommonResult<List<XTbGenInstanceRespVO>> getXTbGenInstanceList(@RequestParam("ids") Collection<String> ids) {
        List<XTbGenInstanceDO> list = xTbGenInstanceService.getXTbGenInstanceList(ids);
        return success(XTbGenInstanceConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得代码生成示例分页")
    @PreAuthorize("@ss.hasPermission('xbh:XT-b-gen-instance:query')")
    public CommonResult<PageResult<XTbGenInstanceRespVO>> getXTbGenInstancePage(@Valid XTbGenInstancePageReqVO pageVO) {
        PageResult<XTbGenInstanceDO> pageResult = xTbGenInstanceService.getXTbGenInstancePage(pageVO);
        return success(XTbGenInstanceConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出代码生成示例 Excel")
    @PreAuthorize("@ss.hasPermission('xbh:XT-b-gen-instance:export')")
    @OperateLog(type = EXPORT)
    public void exportXTbGenInstanceExcel(@Valid XTbGenInstanceExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<XTbGenInstanceDO> list = xTbGenInstanceService.getXTbGenInstanceList(exportReqVO);
        // 导出 Excel
        List<XTbGenInstanceExcelVO> datas = XTbGenInstanceConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "代码生成示例.xls", "数据", XTbGenInstanceExcelVO.class, datas);
    }

}
